import { defineStore } from 'pinia'
import { getUserInfo } from '@/api/user'
import { ref } from 'vue'
import type { UserToken } from '@/model'

// 用户模块
export const UseUserStore = defineStore(
  'user-store',
  () => {
    // 用户信息
    const user = ref<UserToken>({
      empId: 0,
      username: '',
      email: '',
      headImg: '',
      token: ''
    })

    const getUser = async () => {
      const res = await getUserInfo()
      user.value = res.data.data
    }

    const setUser = (newUser: UserToken) => {
      user.value = newUser
    }

    return { user, getUser, setUser }
  },
  {
    persist: true // 持久化
  }
)
